// Create the namespace for Web Pages
TreToday.createNS("TreToday.Web");

TreToday.Web.AdNews = new function () {
    this.init = function () {
//        $("#btnPost").fancybox({
//            'type': 'iframe'
//        });


        $("#ajax_adcatalog").change(function () {
            var $id = $(this).val();
            $.ajax({
                'url': TreToday.baseUrl + '/ajax/getAdNewsByCatalogId?id=' + $id,
                'method': 'GET',
                'success': function ($results) {
                    if ($results.length > 0) {
                        var $html = '';
                        var $wrapper = $("#ajax_adnews").find('ul');
                        for (var i = 0; i < $results.length; i++) {
                            $html += "<li><a href=" + $results[i]['url'] + ">" + $results[i]['name'] + " </a></li>";
                        }

                        $wrapper.html("");
                        $wrapper.append($html);
                    }
                }
            });

        });
    }
}
TreToday.Web.Home = new function () {
    this.init = function () {
        $(".soc_li").hover(function () {
            var $div = $(this).find('.soc_menu_content');
            $div.attr("style", "display:block");
        }, function () {
            $wrapper = $('.soc_li');
            for (var i = 0; i < $wrapper.length; i++) {
                if (!$($wrapper[i]).hasClass('soc_box_js_act')) {
                    var $div = $($wrapper[i]).find('.soc_menu_content');
                    $div.attr("style", "display:none");
                }
            }
        });

        $("#btnSubmit").click(function () {
            search();
        });

        $("#txtSearch").bind("keypress", {}, keypressInBox);

        function keypressInBox (e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            if (code == 13) { //Enter keycode
                search();
            }
        };

        function search () {
            var $text = $("#txtSearch").val().trim();
            if ($text == "")return;
            var $arr = $text.split(" ");
            var $str = new Array();
            for (var i = 0; i < $arr.length; i++) {
                if ($arr[i] == "")continue;
                $str.push($arr[i].trim());
            }
            $text = $str.join('-');
            document.location = TreToday.baseUrl + '/news/search/' + $text;
        };
    }
}
TreToday.Web.News = new function () {
    this.init = function () {
//        $("#btnPost").fancybox({
//            'type': 'iframe'
//        });

        var $i = 11;
        setInterval(function () {
            $(ajax_new_news[$i]).fadeIn(2000).insertBefore($('.ajax_new_news_li:first')).css("font-weight", 700);
            $('.ajax_new_news_li:last').remove();
            $('.ajax_new_news_li:first').next().css("font-weight", 400);
            $i = (parseInt($i) + 1) % $ajax_new_news.length;
        }, 10000);
    }
}
TreToday.Web.Footer = new function () {
    this.init = function () {
        $('#id_footer').cycle({
            fx: 'fade',
            speed: 'fast',
            timeout: 0,
            next: '#next_footer',
            prev: '#prev_footer'
        });
    }
}
TreToday.Web.AdNewsPost = new function () {
    this.init = function () {
        KindEditor.ready(function (K) {
            window.editor = K.create('#txtContent', {
                langType: 'en',
                resizeMode: 1,
                items: [
                    'bold', 'italic', 'underline',
                    'removeformat', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                    'insertunorderedlist', 'image', 'link', 'unlink']
            });
        });

        $("#btnSubmit").click(function () {
            var $catalogid = $("#ddlCatalog").val();
            var $title = $("#txtTitle").val();
            var $content = window.editor.html();
            var $email = $("#txtEmail").val();
            var $data = {'catalogid': $catalogid, 'title': $title, 'content': $content, 'email': $email};
            TreToday.Helpers.block();
            $.ajax({
                type: 'POST',
                url: TreToday.baseUrl + '/adNews/post',
                data: $data,
                success: function (data) {
                    if (data.errors) {
                        TreToday.Helpers.unblock();
                        var $wrapper = $("#divErrors");
                        var $html = '';
                        $.each(data.errors, function ($key, $value) {
                            $html += ' - ' + $value + '<br/>';
                        });
                        $wrapper.attr('style', 'display:block;');
                        $wrapper.html($html);
                        window.location.href = "#divErrors";
                    }
                    else {
                        TreToday.Helpers.unblock();
                        parent.$.fancybox.close();
                        alert("Tin của bạn đã được gửi và chờ kiểm duyệt.")
                    }
                }
            });
        });
    }
}
TreToday.Web.NewsPost = new function () {
    this.init = function () {
        KindEditor.ready(function (K) {
            window.editor = K.create('#txtContent', {
                langType: 'en',
                resizeMode: 1,
                items: [
                    'bold', 'italic', 'underline',
                    'removeformat', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                    'insertunorderedlist', 'image', 'link', 'unlink']
            });
        });

        $("#btnSubmit").click(function () {
            var $catalogid = $("#ddlCatalog").val();
            var $title = $("#txtTitle").val();
            var $short = $("#txtShort").val();
            var $content = window.editor.html();
            var $user = $("#txtUser").val();
            var $data = {'catalogid': $catalogid, 'title': $title, 'short': $short, 'content': $content, 'user': $user};
            TreToday.Helpers.block();
            $.ajax({
                type: 'POST',
                url: TreToday.baseUrl + '/news/post',
                data: $data,
                success: function (data) {
                    if (data.errors) {
                        TreToday.Helpers.unblock();
                        var $wrapper = $("#divErrors");
                        var $html = '';
                        $.each(data.errors, function ($key, $value) {
                            $html += ' - ' + $value + '<br/>';
                        });
                        $wrapper.attr('style', 'display:block;');
                        $wrapper.html($html);
                        window.location.href = "#divErrors";
                    }
                    else {
                        TreToday.Helpers.unblock();
                        parent.$.fancybox.close();
                        alert("Tin của bạn đã được gửi và chờ kiểm duyệt.")
                    }
                }
            });
        });
    }
}

TreToday.Web.displayErrors = function (errors, modelName) {
    if (errors) {
        $.each(errors, function (key, value) {
            $("#" + modelName + "_" + key + "_em_").show().html(value);
            $("#" + modelName + "_" + key).addClass("has-error");
        });
    }
};